package rule

import "strconv"

func isNumeric(value any) bool {
	if value == nil || value == "" {
		return false
	}
	switch value.(type) {
	case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64:
		return true
	case string:
		str := value.(string)
		var (
			// 点
			isDot bool
			// 负数
			isNegativeNumber bool
		)

		for i, ch := range str {
			if i == 0 && ch == '-' {
				if isNegativeNumber {
					return false
				}
				isNegativeNumber = true
				continue
			}
			if ch == '.' {
				if isDot {
					return false
				}
				isDot = true
				continue
			}
			if !(ch >= '0' && ch <= '9') {
				return false
			}
		}
		return true
	}
	return false
}

func IsInt(value any) bool {
	switch value.(type) {
	case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
		return true
	case string:
		return IsIntStr(value.(string))
	default:
		return false
	}
}

func IsIntStr(value string) bool {
	_, err := strconv.Atoi(value)
	return err == nil
}
